package org.dromara.system.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.EmailTemplateVo;
import org.dromara.system.domain.bo.EmailTemplateBo;
import org.dromara.system.service.IEmailTemplateService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 邮件模板
 * 前端访问路由地址为:/system/EmailTemplate
 *
 * @author Gavino
 * @date 2024-11-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/EmailTemplate")
public class EmailTemplateController extends BaseController {

    private final IEmailTemplateService emailTemplateService;

    /**
     * 查询邮件模板列表
     */
    @SaCheckPermission("system:EmailTemplate:list")
    @GetMapping("/list")
    public TableDataInfo<EmailTemplateVo> list(EmailTemplateBo bo, PageQuery pageQuery) {
        return emailTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出邮件模板列表
     */
    @SaCheckPermission("system:EmailTemplate:list")
    @GetMapping("/selectList")
    public R<?> selectList(EmailTemplateBo bo) {
        List<EmailTemplateVo> list = emailTemplateService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出邮件模板列表
     */
    @SaCheckPermission("system:EmailTemplate:export")
    @Log(title = "邮件模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(EmailTemplateBo bo, HttpServletResponse response) {
        List<EmailTemplateVo> list = emailTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "邮件模板", EmailTemplateVo.class, response);
    }

    /**
     * 获取邮件模板详细信息
     *
     * @param templateId 主键
     */
    @SaCheckPermission("system:EmailTemplate:query")
    @GetMapping("/{templateId}")
    public R<EmailTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long templateId) {
        return R.ok(emailTemplateService.queryById(templateId));
    }

    /**
     * 新增邮件模板
     */
    @SaCheckPermission("system:EmailTemplate:add")
    @Log(title = "邮件模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody EmailTemplateBo bo) {
        try {
            bo.setTemplate(URLDecoder.decode(bo.getTemplate(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return R.fail("网页模板解析出错，请检查");
        }
        if(bo.getType() == 1) {
            bo.setStatus(0);
        } else if(bo.getType() == 2) {
            bo.setStatus(1);
        }

        return toAjax(emailTemplateService.insertByBo(bo));
    }

    /**
     * 修改邮件模板
     */
    @SaCheckPermission("system:EmailTemplate:edit")
    @Log(title = "邮件模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody EmailTemplateBo bo) {
        try {
            bo.setTemplate(URLDecoder.decode(bo.getTemplate(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return R.fail("网页模板解析出错，请检查");
        }
        bo.setStatus(null);

        return toAjax(emailTemplateService.updateByBo(bo));
    }

    /**
     * 删除邮件模板
     *
     * @param templateIds 主键串
     */
    @SaCheckPermission("system:EmailTemplate:remove")
    @Log(title = "邮件模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] templateIds) {
        return toAjax(emailTemplateService.deleteWithValidByIds(List.of(templateIds), true));
    }
}
